//
//  RootViewController.m
//  DESCryptoServiceProvider
//
//  Created by yitudev on 13-11-21.
//  Copyright (c) 2013年 MONO. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@property (nonatomic, strong) UITextView *plainTextView;
@property (nonatomic, strong) UIButton *encryptButton;
@property (nonatomic, strong) UIButton *decryptButton;
@property (nonatomic, strong) UITextView *cipherTextView;

@end

@implementation RootViewController

- (void)dealloc
{
    [self.plainTextView removeFromSuperview];
    self.plainTextView = nil;
    
    [self.encryptButton removeFromSuperview];
    self.encryptButton = nil;
    
    [self.decryptButton removeFromSuperview];
    self.decryptButton = nil;
    
    [self.cipherTextView removeFromSuperview];
    self.cipherTextView = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = @"C# JAVA ObjC 互通 DES 算法";
    
    NSString *udid = [SvUDIDTools UDID];
    //FCD51C77-209A-4A1F-914C-EF93C7778C88
    NSLog(@"UDID：%@", udid);
    
    CGFloat y = (iosVerion >= 7.0 ? 20.0 : 0.0) + 49.0;
    
    UILabel *plainLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, y + 10.0, self.view.frame.size.width - 20.0, 0.0)];
    plainLabel.opaque = NO;
    plainLabel.backgroundColor = [UIColor clearColor];
    plainLabel.font = [UIFont systemFontOfSize:14.0];
    plainLabel.textColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];
    plainLabel.text = @"原文：";
    [plainLabel sizeToFit];
    [self.view addSubview:plainLabel];
    
    self.plainTextView = [[UITextView alloc] initWithFrame:CGRectMake(10.0, plainLabel.frame.origin.y + plainLabel.frame.size.height + 10.0, self.view.frame.size.width - 80.0, 100.0)];
    self.plainTextView.opaque = NO;
    self.plainTextView.backgroundColor = [UIColor clearColor];
    self.plainTextView.font = [UIFont systemFontOfSize:14.0];
    self.plainTextView.autocorrectionType = UITextAutocorrectionTypeNo;
    self.plainTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.plainTextView.layer.borderColor = [UIColor colorWithRed:0.00 green:0.00 blue:1.00 alpha:1.00].CGColor;
    self.plainTextView.layer.borderWidth = 0.5;
    self.plainTextView.layer.cornerRadius = 3.0;
    self.plainTextView.text = @"<auth_info req_send_time=\"2013/12/5\" username=\"sjld\" password=\"sjld@323#\" ip=\"ip\" mac=\"mac\" />";
    [self.view addSubview:self.plainTextView];
    
    self.encryptButton = [[UIButton alloc] initWithFrame:CGRectMake(self.plainTextView.frame.origin.x +self.plainTextView.frame.size.width + 5.0, self.plainTextView.frame.origin.y, 60.0, 100.0)];
    self.encryptButton.opaque = NO;
    self.encryptButton.tag = 0;
    self.encryptButton.backgroundColor = [UIColor clearColor];
    self.encryptButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    [self.encryptButton setTitleColor:[UIColor colorWithRed:1.00 green:0.00 blue:0.00 alpha:1.00] forState:UIControlStateNormal];
    [self.encryptButton setTitleColor:[UIColor colorWithRed:0.00 green:0.25 blue:0.50 alpha:1.00] forState:UIControlStateHighlighted];
    [self.encryptButton setTitle:@"加密" forState:UIControlStateNormal];
    [self.encryptButton addTarget:self action:@selector(pressedButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.encryptButton];
    
    UILabel *cipherLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, self.plainTextView.frame.origin.y + self.plainTextView.frame.size.height + 20.0, self.view.frame.size.width - 20.0, 0.0)];
    cipherLabel.opaque = NO;
    cipherLabel.backgroundColor = [UIColor clearColor];
    cipherLabel.font = [UIFont systemFontOfSize:14.0];
    cipherLabel.textColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];
    cipherLabel.text = @"密文：";
    [cipherLabel sizeToFit];
    [self.view addSubview:cipherLabel];
    
    self.cipherTextView = [[UITextView alloc] initWithFrame:CGRectMake(10.0, cipherLabel.frame.origin.y + cipherLabel.frame.size.height + 10.0, self.view.frame.size.width - 80.0, 100.0)];
    self.cipherTextView.opaque = NO;
    self.cipherTextView.backgroundColor = [UIColor clearColor];
    self.cipherTextView.font = [UIFont systemFontOfSize:14.0];
    self.cipherTextView.autocorrectionType = UITextAutocorrectionTypeNo;
    self.cipherTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.cipherTextView.layer.borderColor = [UIColor colorWithRed:1.00 green:0.00 blue:0.00 alpha:1.00].CGColor;
    self.cipherTextView.layer.borderWidth = 0.5;
    self.cipherTextView.layer.cornerRadius = 3.0;
    self.cipherTextView.text = [@"4b4efc24a2ad4af00c59aa7fb081a97787a2a615e8074301f047c5b85a20b33fd1f28ee1624280a6749df8af4573972aaa5d2e4d8a36d9bda06463ed62ba773c553e9e5d14775683fc60c7b2876251f864f0fbed08d5d2797c17cf14703a86ff88e068d46aa87b350eb11587b52f0a1ec48b9e6bc0edf378b3c156b27c9bca80850e24e34707328a78dd08bfe2be3d7a2b377229882f4b2b6712dc85aaef063dc31456a896e7a0a6fb2bad154242bc8d074b0949a6f961956a22264cdf7327794c3b35e2ed0fbeb641fbdad745af4ab933eca30063a2d0bf8390ec441d782d1c9af6c75f68d1d249be3882aa377eb7f55b3e0a7affd486cf7ac9b07aa9f76613e652e74ed6d1be942043d9191de6a6726863382ba660b92648f826c74367a7359f6b66e6d121f0a44a1a70f6194085a4c920880f0e853683bea005ac624f8d9746dd733dda28ad7c3f0a54faa578245af14ad6ba3e53203f5356063e733c8dd8cf9ccc904113f780af7dd89d4c091a4127a8e6cb7adcc55b93a4656ba220e76ea2633d5ddac6cb103b24f5a272ec2940ff29ca7e5a1d5941ca9726ca410da6b52d05b275ec837866d7fdef9a720260c5b6ec0c5daeffed2a02d8da3e81ce728331786fe9290122808c86ef198e6b30d9e7fe227bb563f5c64f19ce2cb76d04238a52bcd468f055db653021a6e15f4d7c4bd683c379e759fd0a520d56c3051dd42a34b3bbc2f8cbfc094cf902a080bd2e8c5e82614dab392782d8462d1f88be94f18f49cd6f9bf6398285781c4fe5ae011ab6591c232b31b3728179bf7e06c1b01999b486b017080fc98f5a959a54fde9dc7af240d14d0b0b5864d8ddbdc1700eb8f2e0436243d0f08a3717ec1e931ace63d5f1040d446eb95d02664db748849d0513888428bc2ad4db536ebd2d460b0ed9c6b89449699e93ba1cc8d62d6458693ce0798c8abe6a6ed1f25375c3f2cf97ebee887f99d234430f8f688892e8955636f29559d9fe037ff9d64acd70ce9b3ffd3c74d711ab6da455e04b3b8bccd2191dfab040d899cfd8f3d56820a15c6f49298d95f24ddc4675cc25a8e2819866eb774721bd6fcdd60b8830d07060dadbe31ca9a8e54057dc31d41d290ee4a5383ff74516d109be5727490325db7e27d283187a6125be438080e6b753a6d469005eca1cf8b980bd0b80177adf2d4be38dc8f30a2ad8bbe60645f666be7a86f9af63a28e1d0bfa0a77f162d6452486720fc58e9666dc8468de2e551d7650910d4e3c9f1f69708c51705230b0ff2218fccbb3a9083421c061e3fe156c77f2df4ba1b0540ce257e407724adeeb751b5f4d41c6b78863d2f9b1cf4ad55a1161dcfc2b4004385c7ef2ffc2cf6cfff2dbffeaf679f840efcdfd9a63c473ff9e955daee978548150b53a457e60f072ed5dd45e5017dc1496ddac652de8b3934db4ccb0618e077b27bcbc13e0346f05d48d0d57d131bd57665a7b4c95864ea7b87fb9d69adcf0bc12cfab6acf54e835ea0111639d0d2db6bd90363f6c51bb01e7766140e4cac26d65bd9d02b12bfcb108e4c2a2ebe4776b19ea08694d4199de09f2c071e0ca16ff4e672164c55c278e36c4a84e003bbfb1d96263c538f1a42d998458c4e32c74db8911f4be7741c5929574b7881d5f1fb8f90bfe14bf805949f7eae88d3b1fdb4ff075697c3bc23873b670ec1d29a953436783c7b483c3611297dfc0dbec982d84c542718e77dc3dc00b27112dae4c2bbe46f0877f00b4af26d27de2c749f47931aaeaf628b1996c3162c2bbd90d5b987a2b6766d6302ae52717d82c21fe80fe12a2ce8972d2b851b675568e24530c20f1bfdabc2656b4bdb580515fafbe97a0a8db34c3c37ba5989e9e210cb8809878648b8e01215c94d5a39a9e64870e882e81a502020f5c3bf6cb579a1ddf104d92476361c73b00df8222ab57d211c54a05ee9a00f350f5cdac4a26483bd1162af5406cf9331d656c22339b1dc9eae967729571cad910af0e34892f8af9d12fe16769b53f96b1d097aa3e46d00579e8effdaacada7566985d290b38e69dd966db0aa1211b0b7ee64ca1f80a11088ed2950c67ac5def709b6f77667e01f86e0f3dff8277c900ad404a001462add969bdc10a8714fd6134c4cfc196c587304015e11bfb54bef63b18c2d7fd3dabf1ce64f75101f8fdf8e0d6e2b14108e1f6961907aee241b7d8e984d1779d43623d99bb2e28b059684a7990f523d056dc1d668769828e797c665347e2bf61e2d201ee55cbf034484fc246e47019661a38973a93fa9ffbf179332e1dba" uppercaseString];
    [self.view addSubview:self.cipherTextView];
    
    self.decryptButton = [[UIButton alloc] initWithFrame:CGRectMake(self.cipherTextView.frame.origin.x +self.cipherTextView.frame.size.width + 5.0, self.cipherTextView.frame.origin.y, 60.0, 100.0)];
    self.decryptButton.opaque = NO;
    self.decryptButton.tag = 1;
    self.decryptButton.backgroundColor = [UIColor clearColor];
    self.decryptButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    [self.decryptButton setTitleColor:[UIColor colorWithRed:0.00 green:0.00 blue:1.00 alpha:1.00] forState:UIControlStateNormal];
    [self.decryptButton setTitleColor:[UIColor colorWithRed:0.00 green:0.25 blue:0.50 alpha:1.00] forState:UIControlStateHighlighted];
    [self.decryptButton setTitle:@"解密" forState:UIControlStateNormal];
    [self.decryptButton addTarget:self action:@selector(pressedButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.decryptButton];
}

- (void)pressedButton:(UIButton *)sender
{
    switch (sender.tag) {
        case 0:
        {
            if (self.plainTextView.text.length < 1) {
                return;
            }
            
            self.cipherTextView.text = @"";
            self.cipherTextView.text = [DESCrypt encrypt2:self.plainTextView.text encryptKey:@"MoU7K*z#"];
            break;
        }
        case 1:
        {
            if (self.cipherTextView.text.length < 1) {
                return;
            }
            
            self.plainTextView.text = @"";
            self.plainTextView.text = [DESCrypt decrypt2:self.cipherTextView.text encryptKey:@"MoU7K*z#"];
            break;
        }
    }
}

@end
